//
//  Kylin_MeFooterView.m
//  03-BuDeJie-2 TabBar设置
//
//  Created by Kylin on 16/2/11.
//  Copyright © 2016年 kylin. All rights reserved.
//

#import "Kylin_MeFooterView.h"
#import <AFNetworking.h>
#import "Kylin_MeSpuares.h"
#import <MJExtension.h>
#import "Kylin_SquareButton.h"
#import "Kylin_WebViewController.h"

@interface Kylin_MeFooterView()

/**模型*/
@property(nonatomic,strong)NSMutableArray *squares;

@end

#define Kylin_ItemCount self.squares.count
static NSInteger const  colsOfRow = 4;
@implementation Kylin_MeFooterView

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        // 请求参数
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"a"] = @"square";
        params[@"c"] = @"topic";
        
        // 发送请求
        Kylin_WeakSelf
        
        [[AFHTTPSessionManager manager] GET:Kylin_RequestURL parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            self.squares = [Kylin_MeSpuares mj_objectArrayWithKeyValuesArray:responseObject[@"square_list"]];
            
              [self resolveDate];
            
            [weakSelf createSquares:_squares];
            
          
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
        
    }
    return self;
}

#pragma mark- 给空白区域加上占位数据
- (void)resolveDate{
    
    NSInteger extrema = Kylin_ItemCount % colsOfRow;
    if (extrema) {
        extrema = colsOfRow - extrema;
        for (NSInteger i = 0; i < extrema; i ++) {
            Kylin_MeSpuares *item = [[Kylin_MeSpuares alloc]init];
            [self.squares addObject:item];
          
        }
    }
    
}

#pragma mark- 创建按钮传送数据
- (void)createSquares:(NSArray *)squares{

    // 按钮尺寸
    CGFloat buttonW = self.width / colsOfRow;
    CGFloat buttonH = buttonW;
    for (NSUInteger i = 0; i < Kylin_ItemCount; i ++) {
        
        // 创建按钮
        Kylin_SquareButton *button = [Kylin_SquareButton buttonWithType:UIButtonTypeCustom];
        
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:button];
        
    
        // 设置frame
        CGFloat buttonX = (i % colsOfRow) * buttonW;
        CGFloat buttonY = (i / colsOfRow) * buttonH;
        
        
        // 第一种方法尺寸减小1
//        button.frame = CGRectMake(buttonX, buttonY, buttonW -1, buttonH -1);
        button.frame = CGRectMake(buttonX, buttonY, buttonW , buttonH );
        
        // 设置模型数据
        button.square = squares [i];
      
        
        // 每一次方法设置footerView 的值
//        self.height = CGRectGetMaxY(button.frame);
        // 总页数 == (总个数 + 每页的个数 - 1) / 每页的个数
        // 第二种方法公式计算法
        NSUInteger rowsCount = (Kylin_ItemCount + colsOfRow - 1) / colsOfRow;
        self.height = rowsCount * buttonH ;
    }
    
    UITableView *tableView = (UITableView *) self.superview;
    tableView.tableFooterView = self;
}


#pragma mark- 按钮点击事件处理
- (void)buttonClick:(Kylin_SquareButton *)button{
   
    if ([button.square.url hasPrefix:@"http"]) {
        
        Kylin_WebViewController *webVC = [[Kylin_WebViewController alloc]init];
        
        // 传值，将按钮的值传给web
        webVC.square = button.square;
        
        // 取出当前选中的导航控制器
        UITabBarController *rootVC = (UITabBarController *) self.window.rootViewController;
        
        // 获取当前选中的VC
        UINavigationController *nav = (UINavigationController *)rootVC.selectedViewController;
        
        [nav pushViewController:webVC animated:YES];
    }
}

/**
 1个控件不能响应点击事件，原因可能有：
 1> userInteractionEnabled = NO;
 2> enabled = NO;
 3> 父控件的userInteractionEnabled = NO;
 4> 父控件的enabled = NO;
 5> 控件已经超出父控件的边框范围
 */
@end
